package com.study.io.nio.demo.reactor;

import java.io.IOException;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

/**
 * 接受类
 *
 * @author wuxj
 */
public class Acceptor implements Runnable {

    final Selector selector;
    final ServerSocketChannel serverSocket;

    /**
     * Acceptor初始化
     *
     * @param selector
     * @param serverSocket
     */
    public Acceptor(Selector selector, ServerSocketChannel serverSocket) {
        this.selector = selector;
        this.serverSocket = serverSocket;
    }

    /**
     * 接受请求 分发handle
     */
    @Override
    public void run() {
        try {
            SocketChannel channel = serverSocket.accept();
            if (channel != null) {
                //new Handler(selector, channel);
                new MthreadHandler(selector, channel);
            }
        } catch (IOException ex) { /* ... */ }
    }
}
